package com.xy.blog.user.controller;

import com.xy.blog.core.exception.BaseException;
import com.xy.blog.core.springmvc.controller.BaseController;
import com.xy.blog.core.springmvc.controller.IBaseController;
import com.xy.blog.core.springmvc.result.page.PageReq;
import com.xy.blog.user.controller.model.req.SysDictReq;
import com.xy.blog.user.controller.model.req.query.SysDictQuery;
import com.xy.blog.user.mapstruct.SysDictConverter;
import com.xy.blog.user.service.impl.SysDictServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/3/6
 */
@Api(value = "SysDictController",tags = "系统字典")
@RequestMapping("/v1/sys-dict")
@RestController
public class SysDictController extends BaseController implements IBaseController<SysDictQuery, SysDictReq> {


    @Autowired
    private SysDictServiceImpl sysDictServiceImpl;

    @Autowired
    private SysDictConverter sysDictConverter;

    @Override
    public Object pages(PageReq<SysDictQuery> req) throws BaseException {
        return getPageData(sysDictConverter.bo2Vo(sysDictServiceImpl.listByPage(req)));
    }

    @Override
    public Object getById(Long id) throws BaseException {
       return getData(sysDictServiceImpl.getById(id));
    }

    @Override
    public Object save(@Valid SysDictReq req) throws BaseException {
        sysDictServiceImpl.save(sysDictConverter.req2Bo(req));
        return success();
    }

    @Override
    public Object update(@Valid SysDictReq req) throws BaseException {
        sysDictServiceImpl.update(sysDictConverter.req2Bo(req));
        return success();
    }

    @Override
    public Object deleteById(Long id) throws BaseException {
        sysDictServiceImpl.deleteById(id);
        return success();
    }

    @GetMapping("/list-all")
    @ApiOperation(value = "查询所有", notes = "查询所有", httpMethod = "GET")
    public Object listAll(String type) throws BaseException {
        return getData(sysDictServiceImpl.listAll(type));
    }
}
